package com.henry.chatkit.conversation

import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import com.blankj.utilcode.util.ResourceUtils
import com.henry.chatkit.R
import com.henry.chatkit.weight.badgeview.BadgeView


open class ConversationCommonHolder(itemView: View) : ConversationBaseHolder(itemView) {

    val conversationIconView: ConversationIconView by lazy { rootView.findViewById(R.id.conversation_icon) }
    val leftItemLayout: RelativeLayout by lazy { rootView.findViewById(R.id.left_item) }
    val titleText: TextView by lazy { rootView.findViewById(R.id.conversation_title) }
    val messageText: TextView by lazy { rootView.findViewById(R.id.conversation_msg) }
    val timeText: TextView by lazy { rootView.findViewById(R.id.conversation_time) }
    val unreadText: BadgeView by lazy { rootView.findViewById(R.id.conversation_unread) }
    val atInfoText: TextView by lazy { rootView.findViewById(R.id.conversation_at_msg) }
    val iconUnreadText: BadgeView by lazy { rootView.findViewById(R.id.conversation_icon_unread) }

    override fun layoutViews(conversation: ConversationInfo?, position: Int) {

        if (conversation == null) {
            return
        }

        if (conversation.top) {
            leftItemLayout.background = ResourceUtils.getDrawable(R.drawable.conversation_item_top)
        } else {
            leftItemLayout.background = ResourceUtils.getDrawable(R.drawable.conversation_item)
        }

        titleText.text = conversation.title
        messageText.text = ""
        timeText.text = ""

        messageText.text = conversation.lastMessageDisplay

        if (conversation.atInfoText == null) {
            atInfoText.visibility = View.GONE
        } else {
            atInfoText.visibility = View.VISIBLE
            atInfoText.text = conversation.atInfoText
        }

        if (conversation.mode == ConversationInfo.ConversationMode.WeChat) {
            iconUnreadText.setNumber(conversation.unRead)
        } else if (conversation.mode == ConversationInfo.ConversationMode.QQ) {
            unreadText.setNumber(conversation.unRead)
        }

        conversationIconView.setConversation(conversation)

    }
}